package com.yc.jpyy.driver.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.yc.jpyy.base.util.PageBean;
import com.yc.jpyy.driver.dao.DriverDao;
import com.yc.jpyy.driver.service.DriverService;
import com.yc.jpyy.utils.UtilDate;

@Service
public class DriverServiceImpl  implements DriverService {
    
	@Autowired
	private DriverDao driverDao;


	@Override
	public int addDriverServer(Map<String, Object> DriverInfo) throws DataAccessException {
		int ret = driverDao.addDriverDao(DriverInfo);
		System.out.println(ret);
		return ret;
	}

	@Override
	public int updateDriverServer(Map<String, Object> DriverInfo) throws DataAccessException {
		int ret = driverDao.updateDriverDao(DriverInfo);
		return ret;
		
	}

	@Override
	public int delDriverServer(Map<String, Object> DriverInfo) throws DataAccessException {
		int ret = driverDao.delDriverDao(DriverInfo);
		return ret;
	}

	@Override
	public Map<String, Object> findDriverByIDServer(Map<String, Object> DriverInfo) throws DataAccessException {
	     
		return driverDao.findDriverByID(DriverInfo);
	}

	

	@Override
	public PageBean findDriverServer(Map<String, Object> paramsMap) throws DataAccessException {
		int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
		int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
		paramsMap.put("pagestart", (pagenum-1) * pagesize);
		paramsMap.put("currdate", UtilDate.getDateFormatter());
		String unitid = Optional.ofNullable(paramsMap.get("unitid")).orElse("").toString();
		paramsMap.put("unitid", unitid.equals("")?"":unitid.split(","));
		List<Map<String, Object>> list = driverDao.findDriverDao(paramsMap);
		PageBean pageBean = new PageBean();
		pageBean.setList(list);
		// 每页几条数据，默认20条
		pageBean.setPageSize(pagesize);
		// 得到数据库中的条数
		pageBean.setAllRow(this.findDriverServerCount(paramsMap));
		// 当前第几页，默认第一页
		pageBean.setCurrentPage(pagenum);
		// 得到一共几页
		pageBean.setTotalPage(pageBean.getTotalPage());
		// 模仿百度分页，只显示10条数据
		pageBean.paginationTool(pagenum, pageBean.getTotalPage());
		return pageBean;
	}

	@Override
	public PageBean findDriverNoPicture(Map<String, Object> paramsMap) throws DataAccessException {
		int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
		int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
		paramsMap.put("pagestart", (pagenum-1) * pagesize);
		paramsMap.put("currdate", UtilDate.getDateFormatter());
		String unitid = Optional.ofNullable(paramsMap.get("unitid")).orElse("").toString();
		paramsMap.put("unitid", unitid.equals("")?"":unitid.split(","));
		List<Map<String, Object>> list = driverDao.findDriverNoPicture(paramsMap);
		PageBean pageBean = new PageBean();
		pageBean.setList(list);
		// 每页几条数据，默认20条
		pageBean.setPageSize(pagesize);
		// 得到数据库中的条数
		pageBean.setAllRow(this.findDriverServerCount(paramsMap));
		// 当前第几页，默认第一页
		pageBean.setCurrentPage(pagenum);
		// 得到一共几页
		pageBean.setTotalPage(pageBean.getTotalPage());
		// 模仿百度分页，只显示10条数据
		pageBean.paginationTool(pagenum, pageBean.getTotalPage());
		return pageBean;
	}
	
	
	@Override
	public int findDriverServerCount(Map<String, Object> paramsMap) throws DataAccessException {
		return driverDao.findDriverDaoCount(paramsMap);
	}

	@Override
	public int delforUpdateDriverServer(Map<String, Object> paramsMap) throws DataAccessException {
		return driverDao.delforUpdateDriverDao(paramsMap);
	}

	@Override
	public int updateDriverStatusServer(Map<String, Object> paramsMap) throws DataAccessException {
		return driverDao.updateDriverStatusDao(paramsMap);
	}

	@Override
	public int findDriverByIdNumberServer(Map<String, Object> paramsMap) throws DataAccessException {
		return driverDao.findDriverByIdNumberDao(paramsMap) ;
	}

	@Override
	public List<Map<String, Object>> findFWJDKHDServer(Map<String, Object> paramsMap) throws DataAccessException {
		return driverDao.findFWJDKHDDao(paramsMap);
	}

	@Override
	public List<Map<String, Object>> findFWJDKHDforNumServer(Map<String, Object> paramsMap) throws DataAccessException {
		return driverDao.findFWJDKHDforNumDao(paramsMap);
	}

	@Override
	public List<Map<String, Object>> findFWJDKbyIdServer(Map<String, Object> paramsMap) throws DataAccessException {
		return driverDao.findFWJDKbyIdDao(paramsMap);
	}

	@Override
	public List<Map<String, Object>> findDriverfromVehicle(Map<String, Object> paramsMap) throws DataAccessException {
		// TODO Auto-generated method stub
		return driverDao.findDriverfromVehicle(paramsMap);
	}

	@Override
	public List<Map<String, Object>> findVehiclefromDriver(Map<String, Object> paramsMap) throws DataAccessException {
		return driverDao.findVehiclefromDriver(paramsMap);
	}

	@Override
	public Map<String, Object> findDriverByCertif(Map<String, Object> info) throws DataAccessException {
		// TODO Auto-generated method stub
		return driverDao.findDriverByCertif(info);
	}

	@Override
	public List<Map<String, Object>> findDriverListService(Map<String, Object> paramsMap) throws DataAccessException {
		// TODO Auto-generated method stub
		return driverDao.findDriverListDao(paramsMap);
	}

	@Override

	public List<Map<String, Object>> findComplainantCompanyCountNum(
			Map<String, Object> paramsMap) throws DataAccessException {
		// TODO Auto-generated method stub
		return driverDao.findComplainantCompanyCountNum(paramsMap);
	}

	@Override
	public List<Map<String, Object>> findDriveAgeSum(
			Map<String, Object> paramsMap) throws DataAccessException {
		// TODO Auto-generated method stub
		return  driverDao.findDriveAgeSum(paramsMap);
	}

	@Override
	public List<Map<String, Object>> findDriveAgeList(Map<String, Object> paramsMap
			) throws DataAccessException {
		// TODO Auto-generated method stub
		return driverDao.findDriveAgeList(paramsMap);
	}


	public List<Map<String, Object>> findDriverNameAndIdNumberAllService(Map<String, Object> paramsMap)
			throws DataAccessException {
		String compid = Optional.ofNullable(paramsMap.get("compid")).orElse("").toString();
		paramsMap.put("compid", compid.equals("")?"":compid.split(","));
		return driverDao.findDriverNameAndIdNumberAllDao(paramsMap);
	}

	@Override
	public PageBean findDriverPunishmentforWeixinService(Map<String, Object> paramsMap) throws DataAccessException {
		int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
		int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
		paramsMap.put("pagestart", (pagenum-1) * pagesize);
	    List<Map<String, Object>> list = driverDao.findDriverPunishmentforWeixinDao(paramsMap);
	    PageBean pageBean = new PageBean();
		pageBean.setList(list);
		// 每页几条数据，默认20条
		pageBean.setPageSize(pagesize);
		// 得到数据库中的条数
		pageBean.setAllRow(list.size()>0?(int)list.get(0).get("countsize"):0);
		// 当前第几页，默认第一页
		pageBean.setCurrentPage(pagenum);
		// 得到一共几页
		pageBean.setTotalPage(pageBean.getTotalPage());
		// 模仿百度分页，只显示10条数据
		pageBean.paginationTool(pagenum, pageBean.getTotalPage());
		return pageBean;
	}

	@Override
	public int findDriverPunishmentforWeixinCountService(Map<String, Object> paramsMap) throws DataAccessException {
		return driverDao.findDriverPunishmentforWeixinCountDao(paramsMap);
	}

	@Override
	public PageBean findDriverComplaintInfoforWeixinService(Map<String, Object> paramsMap) throws DataAccessException {
		int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
		int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
		paramsMap.put("pagestart", (pagenum-1) * pagesize);
		System.out.println((pagenum-1) * pagesize);
	    List<Map<String, Object>> list = driverDao.findDriverComplaintInfoforWeixinDao(paramsMap);
	    PageBean pageBean = new PageBean();
		pageBean.setList(list);
		// 每页几条数据，默认20条
		pageBean.setPageSize(pagesize);
		// 得到数据库中的条数
		pageBean.setAllRow(list.size()>0?(int)list.get(0).get("countsize"):0);
		// 当前第几页，默认第一页
		pageBean.setCurrentPage(pagenum);
		// 得到一共几页
		pageBean.setTotalPage(pageBean.getTotalPage());
		// 模仿百度分页，只显示10条数据
		pageBean.paginationTool(pagenum, pageBean.getTotalPage());
		return pageBean;
	}

	@Override
	public int findDriverComplaintInfoforWeixinCountService(Map<String, Object> paramsMap) throws DataAccessException {
		// TODO Auto-generated method stub
		return driverDao.findDriverComplaintInfoforWeixinCountDao(paramsMap);
	}

	@Override
	public List<Map<String, Object>> findDriverNameToPunishCountAndComplainCountService(Map<String, Object> paramsMap)
			throws DataAccessException {
		return driverDao.findDriverNameToPunishCountAndComplainCountDao(paramsMap);
	}
	
	@Override
	public Map<String, Object> findDriverDetailByIdWx(
			Map<String, Object> paramsMap) throws DataAccessException {
		Map<String,Object> resultMap = new HashMap<>();
		Map<String,Object> map = driverDao.findDriverDetailByIdWx(paramsMap);
		if(map != null && map.size() > 0){
			resultMap.put("returnCode", 1);
			resultMap.put("ListData",map);
			resultMap.put("returnMsg", "操作成功");
		}else{
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "系统无数据");
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectDriverStatistic(
			Map<String, Object> paramsMap) throws DataAccessException {
		Map<String,Object> resultMap = new HashMap<>();
		int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
		int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
		paramsMap.put("pagestart", (pagenum-1) * pagesize);
		List<Map<String,Object>> list = driverDao.selectDriverStatistic(paramsMap);
		int counts = driverDao.selectDriverStatisticCount(paramsMap);
		if(list != null && list.size() > 0){
			resultMap.put("returnCode", 1);
			resultMap.put("ListData",list);
			resultMap.put("TotalCount",counts);
			resultMap.put("TotalPage",counts % pagesize >0 ? counts/pagesize + 1 : counts/pagesize);
			resultMap.put("returnMsg", "操作成功");
		}else{
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "系统无数据");
		}
		return resultMap;
	}
	
	@Override
	public Map<String, Object> getNotOnlineDriver(
			Map<String, Object> paramsMap) throws DataAccessException {
		Map<String,Object> resultMap = new HashMap<>();
		List<Map<String,Object>> list = driverDao.getNotOnlineDriver(paramsMap);
		if(list != null && list.size() > 0){
			resultMap.put("returnCode", 1);
			resultMap.put("ListData",list);
			resultMap.put("returnMsg", "操作成功");
		}else{
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "系统无数据");
		}
		return resultMap;
	}
	
}
